package org.idea.servlet;

import com.google.gson.Gson;
import org.idea.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author zqx
 * @Date 2023-10-26
 */
@WebServlet("/form.do")
public class FormServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求编码 - 防止获取数据时，发生乱码
        req.setCharacterEncoding("utf-8");

        // 设置响应内容类型及编码 - 防止响应数据时，发生乱码
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User(username,password) ;

        String json = new Gson().toJson(user);

        PrintWriter out = resp.getWriter();
        out.println(json);
        out.flush();
        out.close();


    }
}
